package sedev.shopping.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;
import com.liferay.portal.service.RoleServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;

public class SedevUtil {

	public static enum Order {
		ASC, DESC
	}

	public static enum ModeDisplay {
		All, Display, Hide
	}

	public static void checkPage(Integer currpage, Integer pagesize,
			Long totalrecord) {
		if (pagesize < 1)
			pagesize = 1;
		if (currpage < 1) {
			currpage = 1;
		} else {
			Long totalpage = (totalrecord / pagesize);			
			if((totalrecord % pagesize) > 0){
				totalpage += 1;
			}
			if (currpage > totalpage)
				currpage = totalpage.intValue();
		}
	}

	public static long getTime() {
		long result = 0;
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		result = cal.getTimeInMillis();
		return result;
	}
	
    public static String parseDateToString(Long time,String format){
        Date date=new Date();
        date.setTime(time);
        DateFormat dateformat=new SimpleDateFormat(format);//("dd/MM/yyyy");
        return dateformat.format(date);
    }
    
    public static Long parseDateToLong(String strdate){
        Date date;
        DateFormat dateformat=new SimpleDateFormat("MM/dd/yyyy");
        try{
            date=(Date)dateformat.parse(strdate.toString());
        }catch(ParseException pex){
            date=new Date();
        }
        return (Long)date.getTime();
    }
    
    public static String getGroupName(HttpServletRequest request){
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);
		try{
			Group group = themeDisplay.getLayout().getGroup();
			return group.getName();
		}catch(Exception ex){
			return "";
		}
	}
    
    public static long getOwnerId(HttpServletRequest request){
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);
		try{
			Group group = themeDisplay.getLayout().getGroup();
			User owner = (User) UserLocalServiceUtil.getUserById(group.getCreatorUserId());	
			return owner.getUserId();
		}catch(Exception ex){
			return 0;
		}
	}
    
    public static long getOwnerShopId(HttpServletRequest request){
    	long ownerId = 0;
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);
		try{
			Group group = themeDisplay.getLayout().getGroup();
			ownerId = Long.valueOf(group.getName());
			if(ownerId != group.getCreatorUserId()){
				ownerId = 0;
			}
		}catch(Exception ex){
			ownerId = 0;
		}
		return ownerId;
	}
    
    public static boolean isOwner(HttpServletRequest request){
		long currentUserId = PortalUtil.getUserId(request);
		boolean isOwner = false;

		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);
		try{
			Group group = themeDisplay.getLayout().getGroup();
			User owner = (User) UserLocalServiceUtil.getUserById(group.getCreatorUserId());	
			if (owner.getUserId() == currentUserId)
				isOwner = true;
		}catch(Exception ex){
			isOwner = false;
		}
		return isOwner;	
	}
    
    public static boolean isAdministrator(long userId){
    	boolean isAdmin =false;
    	try {
			List<Role> list = RoleServiceUtil.getUserRoles(userId);
			String name = "";
			for(int index=0; index<list.size();++index){
				name = list.get(index).getName().toLowerCase();
				if(name.equals("administrator")){
					isAdmin = true;
					break;
				}
			}
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    	
    	return isAdmin;
    }
}
